package com.pcbly.web.components.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class TimeOp {
	private static final Logger logger = LogManager.getLogger(TimeOp.class);
	private static Locale US = Locale.US;
	private static int fullStype = 0;
	private static int midStype = 2;
	private static int longStype = 1;
	private static int shortStype = 3;
	public static final int SHORT_DATE_FORMAT = 0;
	public static final int MIDDLE_DATE_FORMAT = 1;
	public static final int LONG_DATE_FORMAT = 2;
	public static final int SHORT_TIME_FORMAT = 3;
	public static final int LONG_TIMESTAMP_FORMAT = 4;
	public static final int LONG_TIME_FORMAT = 5;
	public static final int NOT_MATCHED = -1;
	private static final String dateTimePattern[][] = {
			{ "[0-9]{4}\\-(0[1-9]|10|11|12)\\-([1-2][0-9]|0[1-9]|30|31)",
					"yyyy-MM-dd" },
			{
					"[0-9]{4}\\-(0[1-9]|10|11|12)\\-([1-2][0-9]|0[1-9]|30|31)\\s+([0-1][0-9]|2[0-3])\\:([0-5][0-9])",
					"yyyy-MM-dd HH:mm" },
			{
					"[0-9]{4}\\-(0[1-9]|10|11|12)\\-([1-2][0-9]|0[1-9]|30|31)\\s+([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])",
					"yyyy-MM-dd HH:mm:ss" },
			{ "([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])", "HH:mm:ss" },
			{
					"[0-9]{4}\\-(0[1-9]|10|11|12)\\-([1-2][0-9]|0[1-9]|30|31)\\s+([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])\\.[0-9]",
					"yyyy-MM-dd HH:mm:ss" },
			{ "([0-1][0-9]|2[0-3])\\:([0-5][0-9])\\:([0-5][0-9])\\.[0-9]{3}",
					"HH:mm:ss.SSS" } };

	private static class timeDelay implements Runnable {

		public void run() {
			try {
				Thread.sleep(timeInterVal);
			} catch (InterruptedException e) {
				e.printStackTrace(System.err);
			}
		}

		private long timeInterVal;

		public timeDelay(long i) {
			timeInterVal = 0L;
			timeInterVal = i;
		}
	}

	public TimeOp() {
	}

	public static int findMatchedPatternId(String s) {
		for (int i = 0; i < dateTimePattern.length; i++)
			if (s.matches(dateTimePattern[i][0]))
				return i;

		return -1;
	}

	private static Date convertStrToDateTimeAccordPattern(String s) {
		SimpleDateFormat sdf;
		if (findMatchedPatternId(s) == -1)
			return null;
		try {
			sdf = new SimpleDateFormat(
					dateTimePattern[findMatchedPatternId(s)][1]);
			logger
					.debug("\u53D1\u73B0\u5339\u914D\u7684\u65F6\u95F4\u6A21\u677F:"
							+ dateTimePattern[findMatchedPatternId(s)][1]);
			return sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
			logger
					.warn("\u5C06\u5B57\u7B26\u4E32["
							+ s
							+ "]\u6839\u636E\u63D0\u4F9B\u7684\u6A21\u677F\u8F6C\u6362\u4E3A\u65F6\u95F4\u65F6\u53D1\u751F\u9519\u8BEF\uFF1A"
							+ e.getMessage());
			return null;
		}
	}

	public static Date convertStrToDateTime(String s) {
		return convertStrToDateTimeAccordPattern(s);
	}

	public static String convertDateTimeToString(Date d, int date_format) {
		if (d != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(
					dateTimePattern[date_format][1]);
			logger
					.debug("\u4F7F\u7528\u65F6\u95F4\u6A21\u677F:"
							+ dateTimePattern[date_format][1]
							+ "\u8FDB\u884C\u65F6\u95F4\u5230\u5B57\u7B26\u4E32\u7684\u8F6C\u6362");
			return sdf.format(d);
		} else {
			logger
					.info("\u53D1\u73B0\u65E5\u671F\u7C7B\u578B\u53C2\u6570\u4E3Anull");
			return null;
		}
	}

	public static String getShortFormatedDateTime(Date time) {
		DateFormat fmt = DateFormat.getDateTimeInstance(shortStype, longStype,
				US);
		return fmt.format(time);
	}

	public static String getMidFormatedDateTime(Date time) {
		DateFormat fmt = DateFormat
				.getDateTimeInstance(midStype, longStype, US);
		return fmt.format(time);
	}

	public static String getLongFormatedDateTime(Date time) {
		DateFormat fmt = DateFormat.getDateTimeInstance(longStype, longStype,
				US);
		return fmt.format(time);
	}

	public static String getFullFormatedDateTime(Date time) {
		DateFormat fmt = DateFormat.getDateTimeInstance(fullStype, fullStype,
				US);
		return fmt.format(time);
	}

	public static Date setFullFormatedDateTime(String timeStr) {
		try {
			DateFormat fmt = DateFormat.getDateTimeInstance(fullStype,
					fullStype, US);
			return fmt.parse(timeStr);
		} catch (Exception e) {
			e.printStackTrace(System.err);
			return null;
		}
	}

	public static Date setMidFormatedDateTime(String timeStr) {
		try {
			DateFormat fmt = DateFormat.getDateTimeInstance(midStype, midStype,
					US);
			return fmt.parse(timeStr);
		} catch (Exception e) {
			e.printStackTrace(System.err);
			return null;
		}
	}

	public static Date setShortFormatedDateTime(String timeStr) {
		try {
			DateFormat fmt = DateFormat.getDateTimeInstance(shortStype,
					shortStype, US);
			return fmt.parse(timeStr);
		} catch (Exception e) {
			e.printStackTrace(System.err);
			return null;
		}
	}

	public static Date getNextTime(int interVal) {
		Date now = new Date();
		long nowTime = now.getTime();
		return new Date(nowTime + (long) (interVal * 1000));
	}

	public static String transferDateToString(Date time) {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(time);
		String var_year = String.valueOf(calendar.get(1));
		String var_month = String.valueOf(calendar.get(2) + 1);
		if (var_month.length() == 1)
			var_month = "0" + var_month;
		String var_day = String.valueOf(calendar.get(5));
		if (var_day.length() == 1)
			var_day = "0" + var_day;
		String var_hour = String.valueOf(calendar.get(10));
		if (var_hour.length() == 1)
			var_hour = "0" + var_hour;
		String var_minute = String.valueOf(calendar.get(12));
		if (var_minute.length() == 1)
			var_minute = "0" + var_minute;
		String var_second = String.valueOf(calendar.get(13));
		if (var_second.length() == 1)
			var_second = "0" + var_second;
		String result = var_year + var_month + var_day + var_hour + var_minute
				+ var_second;
		return result;
	}

	public static int getCurrnentYear() {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		return calendar.get(1);
	}

	public static int getCurrnentMonth() {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		return calendar.get(2) + 1;
	}

	public static int getCurrnentDate() {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		return calendar.get(5);
	}

	public static int getCurrnentHour() {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		return calendar.get(10);
	}

	public static int getCurrnentMinute() {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		return calendar.get(12);
	}

	public static int getCurrnentSecond() {
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(new Date());
		return calendar.get(13);
	}

	public static void delay(long interValMs) {
		Thread delayTh = new Thread(new timeDelay(interValMs));
		delayTh.setDaemon(true);
		delayTh.start();
		while (delayTh.isAlive())
			;
	}

	public static long diffTime(Date d2, Date d1) {
		long dl2 = d2.getTime();
		long dl1 = d1.getTime();
		return dl2 - dl1;
	}

}
